/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_DRIVER_H__
#define __EB_DRIVER_H__


#include <kernel/initcall.h>
#include <kernel/kobject.h>


/**
 * @brief match table
 * @compatible: compatible string
 * @data: private data
 * @note compatible string is used to match the device
 */
struct of_device_id {
    char compatible[64];
    const void *data;
};


/**
 * @brief driver struct, description of a driver
 * @name: driver name
 * @kobj: kobject
 * @bus: bus type
 * @of_match_table: match table
 */
struct driver {
    const char      *name;
    struct kobj     kobj;
    struct bus_type *bus;
    struct of_device_id  *of_match_table;
};


#ifdef __cplusplus
extern "C" {
#endif


/**
 * @brief check whether the driver has been registered
 * @param drv the driver
 * @return true or false, true means registered, false means not registered
 */
bool driver_is_registered(struct driver *drv);


/** 
 * @brief get the driver name
 * @param drv the driver
 * @return the driver name
 */
const char *driver_get_name(struct driver *drv);


/**
 * @brief set the driver name
 * @param drv the driver
 * @param name the driver name
 * @return none
 */
void driver_set_name(struct driver *drv, const char *name);


/**
 * @brief register the driver
 * @param drv the driver
 * @return int, 0 means success, others means failed
 */
int driver_register(struct driver *drv);


/**
 * @brief unregister the driver
 * @param drv the driver
 * @return none
 */
void driver_unregister(struct driver *drv);


/**
 * @brief create driver attribute file, it will return a kobj
 * @param dev driver pointer
 * @param name attribute name
 * @param data attribute data
 * @param len attribute data length
 * @return kobject pointer, NULL means failed
 */
#define driver_create_attr(drv, name, data, len)  \
                    kobj_create_attr(&drv->kobj, name, data, len)


/**
 * @brief create driver text attribute file, it will return a kobj
 * @param dev driver pointer
 * @param name attribute name
 * @param text attribute text
 * @return kobject pointer, NULL means failed
 */
#define driver_create_text_attr(drv, name, text)  \
                    kobj_create_text_attr(&drv->kobj, name, text);


#ifdef __cplusplus
}
#endif


#endif //!__EB_DRIVER_H__